package com.mouse.hxsj;

import com.melloware.jintellitype.JIntellitype;
import sun.nio.cs.Surrogate;

import java.io.*;

public class Config {
    private static final String jdkDll = "dll";
    private static final String dll32Name = "JIntellitype.dll";
    private static final String dll64Name = "JIntellitype64.dll";
    private static final String dll32Path = "./dll/" + dll32Name;
    private static final String dll64Path = "./dll/" + dll64Name;

    public static void main(String[] args) {
        initDll();
    }

    public static void initDll() {
        String jdkPath = System.getProperty("java.home");
        System.out.println(jdkPath);
        System.out.println(System.getProperty("java.library.path"));
        File file = new File(jdkPath);
        File dllFile = new File(file.getParent(), jdkDll);
        if (!dllFile.exists()) dllFile.mkdir();
        try {
            //32
            InputStream input32 = new FileInputStream(new File(dll32Path));
            File dll32File = new File(dllFile, dll32Name);
            if (dll32File.exists()) dll32File.delete();
            copyFile(input32, dll32File);
            //64
            InputStream input64 = new FileInputStream(new File(dll64Path));

//            InputStream input64 = Surrogate.Generator.class.getResourceAsStream(dll64Path);
            File dll64File = new File(dllFile, dll64Name);
            if (dll64File.exists()) dll64File.delete();
            copyFile(input64, new File(dllFile, dll64Name));
            //判断JDK位数
            String bit = System.getProperty("os.arch");
            if (bit.contains("64")) {
                System.out.println(dll64File.getAbsolutePath());
                JIntellitype.setLibraryLocation(dll64File.getAbsolutePath());
            } else {
                System.out.println(dll32File.getAbsolutePath());
                JIntellitype.setLibraryLocation(dll32File.getAbsolutePath());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void copyFile(InputStream input, File dest) throws IOException {
        OutputStream output = null;
        try {
            output = new FileOutputStream(dest);
            byte[] buf = new byte[1024];
            int bytesRead;
            while ((bytesRead = input.read(buf)) != -1) {
                output.write(buf, 0, bytesRead);
            }
        } finally {
            input.close();
            output.close();
        }
    }

}
